package org.example.groupwarehouse.service.admin;

import org.example.groupwarehouse.entity.Admin;
import org.example.groupwarehouse.entity.Users;
import org.example.groupwarehouse.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServicrProxy implements AdminService {
    @Autowired
    @Qualifier("adminserviceimpl")
    private AdminService adminService;

    @Autowired
    private LogService logService;

    @Override
    public Admin login(String account, String password) {
        System.out.println("代理者模式");
        Admin admin = adminService.login(account, password);
        logService.logOperation(account, "登录", "", admin != null, "");
        return admin;
    }

    @Override
    public List<Users> getAllUsers() {
        System.out.println("代理者模式 - 获取用户列表");
        List<Users> users = adminService.getAllUsers();
        logService.logOperation("system", "查询用户", "获取所有用户", true, "");
        return users;
    }

    @Override
    public List<Admin> getAllAdmins() {
        System.out.println("代理者模式 - 获取管理员列表");
        List<Admin> admins = adminService.getAllAdmins();
        logService.logOperation("system", "查询管理员", "获取所有管理员", true, "");
        return admins;
    }

    @Override
    public int addAdmin(Admin admin) {
        System.out.println("代理者模式 - 添加管理员");
        int result = adminService.addAdmin(admin);
        logService.logOperation("system", "添加管理员", admin.getAccount(), result > 0, "");
        return result;
    }

    @Override
    public int updateAdminStatus(Integer id, Integer status) {
        System.out.println("代理者模式 - 更新状态");
        int result = adminService.updateAdminStatus(id, status);
        String action = status == 0 ? "禁用管理员" : "启用管理员";
        logService.logOperation("system", action, "ID: " + id, result > 0, "");
        return result;
    }

    @Override
    public void changePassword(Integer id, String newPassword) {
        System.out.println("代理者模式 - 修改密码");
        adminService.changePassword(id, newPassword);
        logService.logOperation("system", "修改密码", "ID: " + id, true, "");
    }

    @Override
    public boolean isAccountExist(String account, Integer excludeId) {
        System.out.println("代理者模式 - 检查账号是否存在");
        try {
            logService.logOperation("system", "检查账号",
                    "账号: " + account + (excludeId != null ? " (排除ID: " + excludeId + ")" : ""),
                    true, "开始检查");

            boolean exists = adminService.isAccountExist(account, excludeId);

            logService.logOperation("system", "检查账号结果",
                    "账号 " + account + (exists ? " 已存在" : " 可用"),
                    true, "检查完成");

            return exists;
        } catch (Exception e) {
            logService.logOperation("system", "检查账号错误",
                    "检查账号出错: " + account,
                    false, e.getMessage());
            throw e;
        }
    }

    @Override
    public List<Users> searchUsers(String account, String eml, String tel) {
        return adminService.searchUsers(account ,eml,tel);
    }

    @Override
    public void batchDelete(Integer id) {
        adminService.batchDelete(id);
    }
}